package com.oracle.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.oracle.common.PageResult;
import com.oracle.mapper.SsxxMapper;
import com.oracle.pojo.Ssxx;
import com.oracle.pojo.SsxxExample;
import com.oracle.service.SsxxService;
import com.oracle.vo.SsxxVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class SsxxServiceImpl implements SsxxService {

    @Autowired(required = false)
    private SsxxMapper ssxxMapper;

    @Override
    public List<Ssxx> findList()
    {
        List<Ssxx> ssxxes=ssxxMapper.selectByExample(null);
        return ssxxes;
    }

    @Override
    public PageResult findPages(Integer pageNo, Integer pageSize, Ssxx ssxx)
    {
        PageHelper.startPage(pageNo,pageSize);

        SsxxExample example = new SsxxExample();
        SsxxExample.Criteria criteria = example.createCriteria();

        if (!ObjectUtils.isEmpty(ssxx.getSsxxId()))
        {
            criteria.andSsxxIdEqualTo(ssxx.getSsxxId());
        }
        if (!ObjectUtils.isEmpty(ssxx.getSsDormitoryid()))
        {
            criteria.andSsDormitoryidEqualTo(ssxx.getSsDormitoryid());
        }
        if (!ObjectUtils.isEmpty(ssxx.getSsDormbuild()))
        {
            criteria.andSsDormbuildEqualTo(ssxx.getSsDormbuild());
        }
        if (!ObjectUtils.isEmpty(ssxx.getSsNum()))
        {
            criteria.andSsNumEqualTo(ssxx.getSsNum());
        }
        if (!ObjectUtils.isEmpty(ssxx.getSsRealnum()))
        {
            criteria.andSsRealnumEqualTo(ssxx.getSsRealnum());
        }
        if (!ObjectUtils.isEmpty(ssxx.getGlyName()))
        {
            criteria.andGlyNameEqualTo(ssxx.getGlyName());
        }

        example.setOrderByClause("ssxx_id");

        Page<Ssxx> ssxxPage = (Page<Ssxx>)ssxxMapper.selectByExample(example);

        List<Ssxx> ssxxList = ssxxPage.getResult();

        List<SsxxVo> glyVoList = buildSsxxVoList(ssxxList);

        return new PageResult(ssxxPage.getTotal(),glyVoList);
    }

    private List<SsxxVo> buildSsxxVoList(List<Ssxx> ssxxList)
    {
        List<SsxxVo> ssxxVoList = new ArrayList<>();
        ssxxList.forEach(ssxx -> {
            SsxxVo ssxxVo2 = new SsxxVo();

            BeanUtils.copyProperties(ssxx,ssxxVo2);

            ssxxVoList.add(ssxxVo2);
        });

        return ssxxVoList;
    }
}
